package pit.com.ordermobilesystem.fragment;

import java.io.UnsupportedEncodingException;

import org.json.JSONArray;
import org.json.JSONObject;

import pit.com.ordermobilesystem.BaseFragment;
import pit.com.ordermobilesystem.R;
import pit.com.ordermobilesystem.BaseFragment.TypeLoadingMore;
import pit.com.ordermobilesystem.ShopActivity;
import pit.com.ordermobilesystem.adapter.StoreAdapter;
import pit.com.ordermobilesystem.fragment.CategoryFragment.LoadingCategory;
import pit.com.ordermobilesystem.http.HttpApiException;
import pit.com.ordermobilesystem.http.OrderSystemHttpService;
import pit.com.ordermobilesystem.provider.OrderMobileProvider;
import pit.com.ordermobilesystem.sharepreference.OrderSharePreference;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class StoreFragment extends BaseFragment{
	View rootView;
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);

	    }

	    @Override
	    public View onCreateView(LayoutInflater inflater, ViewGroup container,
	            Bundle savedInstanceState) {
	         rootView = inflater.inflate(R.layout.fragment_store, container, false);

	        new LoadingStore(TypeLoadingMore.LOADING_FIRST).execute();
	        return rootView;
	    }
	    
		class LoadingStore extends AsyncTask<String, Void, Void> {

			TypeLoadingMore typeLoading;
			Exception exception;
			int response_code;
			JSONArray arrayStore;
			
			public LoadingStore(TypeLoadingMore typeLoading) {
				super();
				this.typeLoading = typeLoading;
			}
			protected void onPreExecute() {
				if(typeLoading==TypeLoadingMore.LOADING_FIRST){
					showProgressDialog(new DialogInterface.OnCancelListener() {
						
						@Override
						public void onCancel(DialogInterface dialog) {
							LoadingStore.this.cancel(true);
							dialog.dismiss();
						}


					});
				}

			}

			protected Void doInBackground(final String... args) {
				OrderSystemHttpService httpService=new OrderSystemHttpService(OrderSharePreference.getHost(getActivity()),OrderSharePreference.getPort(getActivity()));
				try {
					ShopActivity activity=(ShopActivity) getActivity();
					JSONObject result=httpService.requestToService(OrderSystemHttpService.URL_GET_LIST_STORE,"{\"sale_id\":"+activity.getData().getInt("id")+"}");
					if(result.getString("status").equals("ok")){		
						arrayStore=result.getJSONObject("data").getJSONArray("stores");
					}else{
						exception=new Exception(result.getString("error"));
					}

				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					exception=e;
				} catch (HttpApiException e) {
					// TODO Auto-generated catch block
					exception=e;
				} catch (Exception e) {
					// TODO Auto-generated catch block
					exception=e;
				}
				return null;
			}

			protected void onPostExecute(final Void unused) {

					hideProgressDialog();

				
				if(exception!=null){
					showAlert("",exception.getMessage());
				}else{
					StoreAdapter adapter=new StoreAdapter(arrayStore,getActivity());
					ListView lv_store=(ListView) rootView.findViewById(R.id.lv_store);
					lv_store.setAdapter(adapter);
				}
				
			}

		}	    
}
